package com.java.runnable.copycode;

/**
 * @author shkstart
 * @create 2021-01-28 20:00
 */

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

public class ClockFrame extends JFrame {
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss.SSS");
    SimpleDateFormat sdfDistance = new SimpleDateFormat("mm:ss.SSS");
    private JTextField nowTime;
    private JButton startBt;
    private JButton resetBt;
    public long start = 0;
    // private int line=1;//记录行数
    private String beforeTime = "";// 上一次时间
    private JScrollPane countPane;
    private DefaultTableModel defaultModel = null;
    private JTable table;

    public ClockFrame() {
        nowTime = new JTextField("00:00.000");
        startBt = new JButton("开始");
        resetBt = new JButton("计次");

        String[] name = {"序号", "时间", "时间差"};
        String[][] data = new String[1][3];
        defaultModel = new DefaultTableModel(data, name);
        table = new JTable(defaultModel);
        table.setPreferredScrollableViewportSize(new Dimension(400, 400));
        countPane = new JScrollPane(table);
        setLayout(null);
        add(nowTime);
        nowTime.setBounds(0, 0, 400, 80);
        nowTime.setBackground(Color.getHSBColor(88, 89, 185));
        nowTime.setForeground(Color.BLUE);
        Font font = new Font("隶书", Font.BOLD, 50);
        nowTime.setFont(font);
        nowTime.setHorizontalAlignment(JTextField.CENTER);
        nowTime.setVisible(true);
        add(startBt);
        startBt.setBounds(0, 80, 200, 50);
        startBt.setVisible(true);
        add(resetBt);
        resetBt.setBounds(200, 80, 200, 50);
        resetBt.setVisible(true);
        add(countPane);
        countPane.setBounds(0, 130, 400, 400);
        countPane.setVisible(true);
        // add(countTb);
        startBt.addActionListener(new MyActionListener());
        resetBt.addActionListener(new MyActionListener());
    }

    class countTask extends TimerTask {
        @Override
        public void run() {
            SimpleDateFormat sdfDistance = new SimpleDateFormat("mm:ss.SSS");
            nowTime.setText(sdfDistance.format(System.currentTimeMillis() - start));
        }
    }

    class MyActionListener implements ActionListener {

        Date date = new Date();
        Timer timer = null;
        int line = 0;
        long now = 0, bebefore = 0, nowdistance = 0;

        public void actionPerformed(ActionEvent e) {
            start = date.getTime();
            if (e.getActionCommand().equals("开始")) {
                startBt.setText("暂停");
                resetBt.setText("计次");
                //if (timer == null) {
                timer = new Timer();
                timer.schedule(new countTask(), 0, 10);
                //}
            }
            if (e.getActionCommand().equals("暂停")) {
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                try {
                    Date screen = sdfDistance.parse(nowTime.getText());
                    System.out.println("the date toString is " + screen.toString());
                    long screenValue = GetTime(nowTime.getText());
                    System.out.println(screenValue);
                    System.out.println(new Date().getTime());
                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                startBt.setText("开始");
                resetBt.setText("重置");
            }
            if (e.getActionCommand().equals("重置")) {
                nowTime.setText("00:00.000");
                resetBt.setText("计次");
                // timer.cancel();
                // getRowCount返回行数，rowcount<0代表已经没有任何行了。
                int rowcount = defaultModel.getRowCount() - 1;
                for (int i = rowcount - 1; i >= 0; i--) {
                    defaultModel.removeRow(i);
                }
            }
            if (e.getActionCommand().equals("计次")) {
                // 动态添加表格
                defaultModel.addRow(new Vector());
                // 为表格赋值
                ++line;
                defaultModel.setValueAt(line + "", line - 1, 0);
                defaultModel.setValueAt(nowTime.getText(), line - 1, 1);
                // System.out.println("行数："+(line++));
                System.out.println("现在时间：" + nowTime.getText());
                System.out.println("之前时间：" + beforeTime);
                now = GetTime(nowTime.getText());
                bebefore = GetTime(beforeTime);
                nowdistance = now - bebefore;
                defaultModel.setValueAt(sdfDistance.format(nowdistance) + "", line - 1, 2);
                System.out.println("现在时间转换成：" + now);
                System.out.println("之前时间转换成：" + bebefore);
                System.out.println("时间差：" + nowdistance);
                System.out.println("时间差转化成" + sdfDistance.format(nowdistance));
                System.out.println("测试能不能输出");
                System.out.println();
                beforeTime = nowTime.getText();
            }
            // 刷新表格
            table.revalidate();
        }
    }

    public long GetTime(String time) {
        long before = 0;
        if ((time == "") || (time == "00:00.000")) {
            before = 0;
        }
        // subString（start,end）,取从start到end 字符串
        else {
            int front = Integer.parseInt(time.substring(0, 2));
            int middle = Integer.parseInt(time.substring(3, 5));
            int rear = Integer.parseInt(time.substring(6, time.length()));
            before = front * 60000 + middle * 1000 + rear;
        }
        return before;
    }
}
